package com.kaigejava.juc.queue;

import com.alibaba.fastjson.JSON;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/**
 * DelayQueue代码演示.
 * 模拟缓存到期时间
 * @author kaigejava
 */
public class DelayQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        MyCache cache1 = new MyCache(1000L,"k1",(Object) 5);
        MyCache cache2 = new MyCache(3000L,"k2",(Object) 10);
        MyCache cache3 = new MyCache(5000L,"k3",(Object) 15);

        DelayQueue dQueue = new DelayQueue();
        dQueue.put(cache1);
        dQueue.put(cache2);
        dQueue.put(cache3);
        System.out.println("==开始获取："+ new Date());
        for (;;) {
            MyCache cache = (MyCache) dQueue.take();
            System.out.format("name:{%s}, time:{%s}\n",JSON.toJSON(cache.getMap()), LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
            System.out.println("==:{}"+JSON.toJSONString(cache));

        }
    }
}
